using System;
using System.Collections;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Globalization;
using Spring.Objects.Factory;

namespace Sedna.Core.Controller
{
	public class ValidatorWrapper : IValidatorWrapper, IObjectNameAware
	{
		private string objectName;
		private string id;
		private string validatorName;
		private string placeHolderId;
//		private IMessageManager messageManager;
//		private string labelKey;
//		private string label;
		private IDictionary overridenProperties;
		private bool ignoreIfPropertyNotFound = false;
		private string serverValidateEventHandlerName;
		private string serverValidateEventName = "ServerValidate";
		private Type serverValidateEventHandlerType = typeof(ServerValidateEventHandler);

		public readonly static string PLACEHOLDER_AUTO = "auto";
		public readonly static string PLACEHOLDER_RIGHT = "right";

		public ValidatorWrapper()
		{
		}


		public string ObjectName
		{
			get { return objectName; }
			set { objectName = value; }
		}

		public string Id
		{
			get {return id!=null?id:objectName;}
			set {id = value;}
		}

//		public string LabelKey
//		{
//			get { return labelKey!=null&&labelKey!=string.Empty?labelKey:ObjectName; }
//			set { labelKey = value; }
//		}

//		public string Label
//		{
//			get { return label; }
//			set { label = value; }
//		}
//
		public string ValidatorName
		{
			get { return validatorName; }
			set { validatorName = value; }
		}

		public string PlaceHolderId
		{
			get { return placeHolderId; }
			set { placeHolderId = value; }
		}

//		public IMessageManager MessageManager
//		{
//			get { return messageManager; }
//			set { messageManager = value; }
//		}
//
//		public string GetLocalizedMessage()
//		{
//			return GetLocalizedMessage(label!=null&&label!=string.Empty?label:validator.ErrorMessage);
//		}
//
//		public string GetLocalizedMessage(string defaultValue)
//		{
//			return messageManager.GetMessage(LabelKey, defaultValue);
//		}


		public IDictionary OverridenProperties
		{
			get { return overridenProperties; }
			set { overridenProperties = value; }
		}

		public bool IgnoreIfPropertyNotFound
		{
			get { return ignoreIfPropertyNotFound; }
			set { ignoreIfPropertyNotFound = value; }
		}

		public string ServerValidateEventHandlerName
		{
			get { return serverValidateEventHandlerName; }
			set { serverValidateEventHandlerName = value; }
		}

		public string ServerValidateEventName
		{
			get { return serverValidateEventName; }
			set { serverValidateEventName = value; }
		}

		public Type ServerValidateEventHandlerType
		{
			get { return serverValidateEventHandlerType; }
			set { serverValidateEventHandlerType = value; }
		}


		public IValidator BuildValidator(IObjectFactory objectFactory)
		{
			IValidator validator = (IValidator)objectFactory.GetObject(validatorName);
			if(overridenProperties == null || overridenProperties.Count == 0)
			{
				return validator;
			}

			Type validatorType = validator.GetType();
			for(IDictionaryEnumerator en = overridenProperties.GetEnumerator(); en.MoveNext();)
			{
				PropertyInfo propertyInfo = validatorType.GetProperty((string)en.Key);
				if(propertyInfo == null && ignoreIfPropertyNotFound)
				{
					continue;
				}
				else if(propertyInfo == null)
				{
					throw new ControllerConfigurationException("unable to find a property named'" + en.Key + "' for a validator, please make sure that a property is named correctly or set up a property 'ignoreIfPropertyNotFound' to false");
				}
				if(propertyInfo.PropertyType.IsEnum)
				{
					propertyInfo.SetValue(validator, Enum.Parse(propertyInfo.PropertyType, (string)en.Value), null);
				}
				else
				{
					propertyInfo.SetValue(validator, en.Value, null);
				}

			}
			return validator;
		}

	}
}
